package cc.mrbird.febs.mobile.controller;

import javax.validation.constraints.NotBlank;

import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import cc.mrbird.febs.common.annotation.IgnoreToken;
import cc.mrbird.febs.common.annotation.Limit;
import cc.mrbird.febs.common.controller.BaseController;
import cc.mrbird.febs.common.entity.FebsConstant;
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.exception.FebsException;
import cc.mrbird.febs.member.entity.Member;
import cc.mrbird.febs.member.service.IMemberService;
import cc.mrbird.febs.monitor.entity.LoginLog;
import cc.mrbird.febs.monitor.service.ILoginLogService;
import cc.mrbird.febs.order.service.IOrderInfoService;
import lombok.RequiredArgsConstructor;

/**
 * 移动端 Controller
 *
 * @author Lord
 * @date 2020-06-012 15:48:19
 */
@Validated
@Controller
@RequestMapping("/mobile")
@RequiredArgsConstructor
public class MobileController extends BaseController {

    private final IMemberService memberService;
    private final ILoginLogService loginLogService;
    private final IOrderInfoService orderInfoService;

    /**
     * 获取短信验证码
     * @param telephone 手机号
     * @return
     */
    @IgnoreToken
    @PostMapping("/captcha/send")
    public FebsResponse captchaSend(@NotBlank(message = "{required}") String telephone) {
        memberService.sendSms(telephone);
        return new FebsResponse().success();
    }

    /**
     * 校验短信验证码
     * @param telephone 手机号
     * @param verifyCode 验证码
     * @return
     */
    @IgnoreToken
    @PostMapping("/captcha/verify")
    public FebsResponse captchaVerify(@NotBlank(message = "{required}") String telephone,
            @NotBlank(message = "{required}") String verifyCode) {
        memberService.verifySms(telephone, verifyCode);
        return new FebsResponse().success();
    }

    /**
     * 短信验证码登录
     * @param telephone 手机号
     * @param verifyCode 验证码
     * @return
     */
    @IgnoreToken
    @PostMapping("login")
    @Limit(key = "login", period = 60, count = 10, name = "登录接口", prefix = "limit")
    public FebsResponse login(@NotBlank(message = "{required}") String telephone,
            @NotBlank(message = "{required}") String verifyCode) throws FebsException {

        Member member = memberService.login(telephone, verifyCode);

        // 将生成的token存在header中传递至前端
        response.setHeader(FebsConstant.TOKEN, member.getToken());

        // 保存登录日志
        LoginLog loginLog = new LoginLog();
        loginLog.setUsername(telephone);
        loginLog.setSystemBrowserInfo();
        this.loginLogService.saveLoginLog(loginLog);

        return new FebsResponse().success().data(member);
    }

    /**
     * 根据token获取会员信息
     * @param request
     * @return
     */
    @GetMapping("token")
    public FebsResponse getMemberByToken() {
        Long memberId = getMemberIdByToken(request);
        Member member = memberService.findMemberDetailById(memberId);
        return new FebsResponse().success().data(member);
    }

}
